----------------------------------------------------------
--	Name: 		Communicator Extension Script	        --
--  Copyright 2013 Mertsch								--
----------------------------------------------------------

local _G = getfenv(0)
local ipairs, pairs, select, string, table, next, type, unpack, tinsert, tconcat, tremove, fmt, tostring, tonumber, tsort = _G.ipairs, _G.pairs, _G.select, _G.string, _G.table, _G.next, _G.type, _G.unpack, _G.table.insert, _G.table.concat, _G.table.remove, _G.string.format, _G.tostring, _G.tonumber, _G.table.sort			
local interface, interfaceName = object, object:GetName()
local HoN_CommunicatorExtension = {}

function HoN_CommunicatorExtension:ChatNewChannel(widget, chanID, chanName)
	println('^555Debug: HoN_CommunicatorExtension:ChatNewChannel(widget="'..tostring(widget)..'" chanID="'..tostring(chanID)..'" chanName="'..tostring(chanName)..'")')
	if (chanName == "") then
		return
	end

	local leaveHon = GetCvarBool('dcl_hon', true)
	if (leaveHon == nil) then leaveHon = false end
	local leaveMatch = GetCvarBool('dcl_match', true)
	if (leaveMatch == nil) then leaveMatch = false end
	--local dcl_group = GetCvarBool('dcl_group', true)
	--if (dcl_group == nil) then dcl_group = false end
	local leaveStream = GetCvarBool('dcl_stream', true)
	if (leaveStream == nil) then leaveStream = false end

	if((leaveHon and (chanName == 'HoN' or string.match(chanName, 'HoN %d+')))
       or
       (leaveMatch and string.match(chanName, 'Match %d+'))
--     or
--     (dcl_group and string.match(chanName, 'Group %d+'))
       or
       (leaveStream and string.match(chanName, 'Stream .+'))
	  )
	then
		println('^gClosing channel "'..chanName..'" (Settings: HoN="'..tostring(leaveHon)..'", Match="'..tostring(leaveMatch)..'", Stream="'..tostring(leaveStream)..'").')
		--Adding a 500ms delay to make it work when the UI is busy creating the channel.
		--interface:UICmd("ChatLeaveChannel('"..chanName.."');")
		interface:UICmd("SleepWidget(500, 'ChatLeaveChannel(\\'"..chanName.."\\');');")
	else
		println('^gRemaining in channel "'..chanName..'" (Settings: HoN="'..tostring(leaveHon)..'", Match="'..tostring(leaveMatch)..'", Stream="'..tostring(leaveStream)..'").')
	end
end

function CommunicatorExtensionInitalize(widget)
	--println('^555Debug: communicatorextensions.lua:CommunicatorExtensionInitalize(widget = '..tostring(widget)..') BEGIN')
	widget:RegisterWatch("ChatNewChannel", function(...) HoN_CommunicatorExtension:ChatNewChannel(...) end)
end